package com.xe.framework;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.Toast;

public class XEActivity extends Activity {

	private Toast mCloseToast;
	private boolean mCloseStatus;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		((XEApp) getApplication()).addActivity(this);
		super.onCreate(savedInstanceState);
		initExit();
	}

	@Override
	protected void onResume() {
		((XEApp) getApplication()).setCurrentActivity(this);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		((XEApp) getApplication()).removeActivity(this);
		super.onDestroy();
	}

	/**
	 * 默认处理了返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// 实现退出功能
			if (mCloseStatus) {
				mCloseToast.cancel();
				finish();
				XEApp.getInstance().exit();
				return true;
			} else {
				mCloseToast.show();
				mCloseStatus = true;
				Handler exitHandler = new Handler();
				exitHandler.postDelayed(new Runnable() {

					public void run() {
						mCloseStatus = false;
					}
				}, 3000);
				return true;
			}
		}
		
		
		return super.onKeyDown(keyCode, event);
	}
	
	private void initExit() {
		mCloseToast = Toast.makeText(this, "亲，一定要来哦！", Toast.LENGTH_SHORT);
		mCloseStatus = false;
	}
}
